package org.szgb.console.ht.electricity.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.ht.electricity.entity.HtElectricitySettlementBillModel;
import org.szgb.console.ht.electricity.mapper.HtElectricitySettlementBillModelMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class HtElectricitySettlementBillModelService {
    private final HtElectricitySettlementBillModelMapper htElectricitySettlementBillModelMapper;

    @Autowired
    public HtElectricitySettlementBillModelService(HtElectricitySettlementBillModelMapper htElectricitySettlementBillModelMapper){
        this.htElectricitySettlementBillModelMapper = htElectricitySettlementBillModelMapper;
    }

    @Transactional(readOnly = true)
    public HtElectricitySettlementBillModel get(String id) {
        return htElectricitySettlementBillModelMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<HtElectricitySettlementBillModel> findList(HtElectricitySettlementBillModel htElectricitySettlementBillModel) {
        if (htElectricitySettlementBillModel != null && htElectricitySettlementBillModel.getPageDto() != null) {
            Page<HtElectricitySettlementBillModel> page = new Page<>(htElectricitySettlementBillModel.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            htElectricitySettlementBillModel.setPage(page);
        }
        return htElectricitySettlementBillModelMapper.findList(htElectricitySettlementBillModel);
    }

    @Transactional(readOnly = true)
    public Integer getCount(HtElectricitySettlementBillModel htElectricitySettlementBillModel) {
        return htElectricitySettlementBillModelMapper.getCount(htElectricitySettlementBillModel);
    }

    @Transactional()
    public void save(HtElectricitySettlementBillModel htElectricitySettlementBillModel) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (htElectricitySettlementBillModel.getId() == null || htElectricitySettlementBillModel.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            htElectricitySettlementBillModel.setCreatedBy(htElectricitySettlementBillModel.getUpdatedBy());
            // 添加UUID
            htElectricitySettlementBillModel.setId(UUIDUtil.getUUID());
            // 执行插入操作
            htElectricitySettlementBillModelMapper.insert(htElectricitySettlementBillModel);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            htElectricitySettlementBillModelMapper.update(htElectricitySettlementBillModel);
        }
    }

    @Transactional
    public void delete(String ids) {
        htElectricitySettlementBillModelMapper.delete(ids.split(","));
    }
}
